<template>
  <div>
    <el-card v-loading="loading" class="box-card box-card-list">
      <div slot="header" class="clearfix">
        <div class="temHeader">
          <span class="iconfont labelIcon" />
          <span class="area-header-title" style="font-weight: bold">{{ $t('标签') }}</span>
          <div class="rt newPic">
            <el-button v-hasPermi="['deal:dealbase:edit']" type="text" @click="tagOpen = true">{{ $t('标签选择') }}</el-button>
          </div>
        </div>
      </div>
      <div class="text item labelItem">
        <div v-if="dynamicTags.length > 0">
          <div v-for="v in dynamicTags" :key="v.configureLabel">
            <p class="configureLabel">{{ v.configureLabel }}</p>
            <el-tag
              v-for="tag in v.data"
              :key="tag.id"
              effect="plain"
              :title="tagFormat(tag)"
              style="background: rgba(67, 78, 184, 0.15);border: 0;line-height: 28px;"
            >{{ tagFormat(tag) }}
            </el-tag>
          </div>
        </div>
        <div v-else class="noneData">
          <div>
            <!-- <svg-icon icon-class="none" class-name="none-icon" /> -->
            <p>{{ $t('暂无内容') }}</p>
          </div>
        </div>

        <!--        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          filterable
          clearable
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button>-->
      </div>
    </el-card>

    <el-dialog
      :title="$t('标签选择')"
      :visible.sync="tagOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="80%"
      append-to-body
      top="60px"
      class="tag-dialog"
    >
      <tag-select v-slot ref="tagSelect" :chosen-tag-data="dynamicTags" :bus-id="dealId" class="tag-content" @close="close" />
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <!--保存 取消按钮-->
        <save-btn :function="submitForm" :cancel-func="close" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import TagSelect from '@/components/Tag/TagSelect'
import { findByDealId } from '@/api/deal/tags/dealTagsRef'

export default {
  components: { TagSelect },
  data() {
    return {
      inputVisible: false,
      inputValue: '',
      loading: false,
      dynamicTags: [],
      type: undefined,
      tagOpen: false,
      dealId: this.$route.query.id
    }
  },
  methods: {
    init(type) {
      this.type = type
      this.loading = true
      findByDealId(this.dealId).then(response => {
        var arr = response.data || []
        this.dynamicTags = this.classify(arr)
        this.loading = false
      })
    },
    classify(arr) {
      const map = {}
      const myArr = []
      for (let i = 0; i < arr.length; i++) {
        if (!map[arr[i].configureLabel]) {
          myArr.push({
            configureLabel: arr[i].configureLabel,
            data: [arr[i]]
          })
          map[arr[i].configureLabel] = arr[i]
        } else {
          for (let j = 0; j < myArr.length; j++) {
            if (arr[i].configureLabel === myArr[j].configureLabel) {
              myArr[j].data.push(arr[i])
              break
            }
          }
        }
      }
      return myArr
    },
    close(refresh) {
      this.tagOpen = false
      if (refresh) {
        this.init()
      }
    },
    tagFormat(item) {
      let result = item.configureLabel
      if (item.firstEdit === 'Y' && item.tagContent) {
        result += ' / ' + item.tagContent
      } else {
        if (item.firstTagLabel) {
          result += ' / ' + item.firstTagLabel
        }
        if (item.secondTagLabel) {
          result += ' / ' + item.secondTagLabel
        }
        if (item.thirdTagLabel) {
          result += ' / ' + item.thirdTagLabel
        }
        if (item.fourthTagLabel) {
          result += ' / ' + item.fourthTagLabel
        }
        if (item.fifthTagLabel) {
          result += ' / ' + item.fifthTagLabel
        }
      }
      return result
    },
    submitForm() {
      this.$nextTick(() => {
        this.$refs.tagSelect.submitForm()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.tag-dialog /deep/ .el-dialog__body {
  height: 100%;
  overflow: auto;
}
.tag-dialog .tag-content {
  height: 600px;
}
/deep/.labelItem{
  .configureLabel{
    font-weight: 400;
    color: #333333;
    margin: 0;
    font-size: 14px;
    line-height: 30px;
  }
  .el-tag{
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    margin-bottom: 2px;
  }
}
</style>
